<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Panier
 *
 * @author Yahn_V
 */
class Panier {

    private $lesDimensions1;

    public function __construct() {
        $this->lesDimensions1 = array();
    }

    public function ImportInto(array $dimensionSelected) {
        foreach ($dimensionSelected as $dim) {

            $dim = unserialize(urldecode($dim));

            //on regarde si la dimension1 existe
            if ($this->EstPresentD1($dim)) {
                //récupération de la position de la dimension1 dans le tableau
                $position = $this->getPosition($dim, $this->lesDimensions1);
                switch (get_class($dim)) {
                    case 'Dimension3':

                        $lesDimensions2 = $this->lesDimensions1[$position]->getLesDimensions2(null);


                        if ($this->EstPresentD2($dim->getDimension2(), $lesDimensions2)) {
                            $positionD2 = $this->getPosition($dim->getDimension2(), $lesDimensions2);
                            $d1 = $this->lesDimensions1[$position];
                            $d2 = $this->lesDimensions1[$position]->getLesDimensions2($positionD2); //new Dimension2($d1, $dim->getDimension2()->getDimension2(), $dim->getDimension2()->getTitre(), $dim->getDimension2()->getDescr());

                            $d3 = new Dimension3($d1, $d2, $dim->getNiveau(), $dim->getDescr());

                            $this->lesDimensions1[$position]->getLesDimensions2($positionD2)->addDimension3($d3);
                        } else {

                            $refD2 = $dim->getDimension2()->getDimension2();
                            $titre = $dim->getDimension2()->getTitre();
                            $descr = $dim->getDimension2()->getDescr();
                            $dim1 = new Dimension1(array("refD1" => $dim->getDimension1()->getRefD1(), "nom" => $dim->getDimension1()->getNom()));
                            $dim2 = new Dimension2($dim1, $refD2, $titre, $descr);
                            $dim3 = new Dimension3($dim1, $dim2, $dim->getNiveau(), $dim->getDescr());


                            $dim2->addDimension3($dim3);
                            $this->lesDimensions1[$position]->addDimension2($dim2);
                        }

                        break;
                    case 'Dimension4' :

                        $lesDimensions2 = $this->lesDimensions1[$position]->getLesDimensions2(null);


                        if ($this->EstPresentD2($dim->getDimension2(), $lesDimensions2)) {
                            $positionD2 = $this->getPosition($dim->getDimension2(), $lesDimensions2);
                            $d1 = $this->lesDimensions1[$position];
                            $d2 = $this->lesDimensions1[$position]->getLesDimensions2($positionD2); //new Dimension2($d1, $dim->getDimension2()->getDimension2(), $dim->getDimension2()->getTitre(), $dim->getDimension2()->getDescr());

                            $d4 = new Dimension4($d1, $d2, $dim->getType(), $dim->getChiffre(), $dim->getDescr());

                            $this->lesDimensions1[$position]->getLesDimensions2($positionD2)->addDimension4($d4);
                        } else {

                            $refD2 = $dim->getDimension2()->getDimension2();
                            $titre = $dim->getDimension2()->getTitre();
                            $descr = $dim->getDimension2()->getDescr();
                            $dim1 = new Dimension1(array("refD1" => $dim->getDimension1()->getRefD1(), "nom" => $dim->getDimension1()->getNom()));
                            $dim2 = new Dimension2($dim1, $refD2, $titre, $descr);
                            $dim4 = new Dimension4($d1, $d2, $dim->getType(), $dim->getChiffre(), $dim->getDescr());


                            $dim2->addDimension3($dim4);
                            $this->lesDimensions1[$position]->addDimension2($dim4);
                        }

                        break;
                }
            } else {
                $refD1 = $dim->getDimension1()->getRefD1();
                $nomD1 = $dim->getDimension1()->getNom();
                $dim1 = new Dimension1(array("refD1" => $refD1, "nom" => $nomD1));
                $refD2 = $dim->getDimension2()->getDimension2();
                $titreD2 = $dim->getDimension2()->getTitre();
                $descrD2 = $dim->getDimension2()->getDescr();
                $dim2 = new Dimension2($dim1, $refD2, $titreD2, $descrD2);
                switch (get_class($dim)) {
                    case 'Dimension3' :

                        $dim3 = new Dimension3($dim1, $dim2, $dim->getNiveau(), $dim->getDescr());
                        $dim2->addDimension3($dim3);

                        break;
                    case 'Dimension4':
                        $dim4 = new Dimension4($dim1, $dim2, $dim->getType(), $dim->getChiffre(), $dim->getDescr());
                        $dim2->addDimension4($dim4);
                        break;
                }


                $dim1->addDimension2($dim2);

                $this->lesDimensions1[] = $dim1;
            }
        }
    }

    public function EstPresentD1($dimension) {

        switch (get_class($dimension)) {
            case 'Dimension3':
                foreach ($this->lesDimensions1 as $lesDims) {
                    if ($lesDims->getRefD1() == $dimension->getDimension1()->getRefD1()) {
                        return true;
                    }
                }
                break;
            case 'Dimension4':

                foreach ($this->lesDimensions1 as $lesDims) {
                    if ($lesDims->getRefD1() == $dimension->getDimension1()->getRefD1()) {
                        return true;
                    }
                }
                break;
        }
        return false;
    }

    public function EstPresentD2($dimension, $tab) {
        foreach ($tab as $laDim) {
            if (($dimension->getDimension2() == $laDim->getDimension2()) && ($dimension->getDimension1()->getRefD1() == $laDim->getDimension1()->getRefD1())) {

                return true;
            }
        }
        return false;
    }

    public function getPosition($dimension, $tab) {
        $pos = -1;
        switch (get_class($dimension)) {
            case 'Dimension3' :

                foreach ($tab as $lesDims) {
                    $pos++;

                    if ($lesDims->getRefD1() == $dimension->getDimension1()->getRefD1()) {


                        return $pos;
                    }
                }
                break;
            case 'Dimension2':

                foreach ($tab as $lesDims) {
                    $pos++;
                    if ($lesDims->getDimension1()->getRefD1() == $dimension->getDimension1()->getRefD1() && $lesDims->getDimension2() == $dimension->getDimension2()) {


                        return $pos;
                    }
                }
                break;
            case 'Dimension4':
                foreach ($tab as $lesDims) {
                    $pos++;

                    if ($lesDims->getRefD1() == $dimension->getDimension1()->getRefD1()) {


                        return $pos;
                    }
                }
                break;
        }

        return null;
    }

    public function getPanier() {
        return $this->lesDimensions1;
    }

}
?>
